package com.cpit.platform.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.cpit.platform.dto.User;

public interface UserRepository extends JpaRepository<User, Long> {
	@SuppressWarnings("unchecked")
	User save(User user);
	User findById(Integer id);
	void delete(User user);
	void deleteById(int id);
    User findByUsername(String username);
   
    @Query("from User u where u.username=:name")
    User findUser(@Param("name") String name);

	/**
	 * 根据roleid 查询用户表
	 * @param roleId
	 * @return
	 */
	@Query("select u from User u , UserToRole utr where utr.userId = u.id and utr.roleId = ?1")
	List<User> findUsersByRoleId(Integer roleId);

	/**
	 * 根据组织机构ID 查询users
	 * @param organizationId
	 * @return
	 */
	List<User> findAllByOrganizationId(int organizationId);
    
    /**
     * 用于区域管理员用户查询数据
     */
    List<User> findByOrganizationCodeStartingWith(String orgCode);
    Page<User> findByUsernameContainingAndOrganizationCodeStartingWith(
    		String username, 
    		String organizationCode, 
    		Pageable pageable);
    
    /**
     * 用于普通用户查询数据
     */
    List<User> findByOrganizationCode(String orgCode);
    Page<User> findByUsernameContainingAndOrganizationCode(
    		String username, 
    		String organizationCode, 
    		Pageable pageable);
   
    /**
     * 用于系统功能管理员查询数据
     */
    Page<User> findByUsernameContaining(
    		String username, 
    		Pageable pageable);
}
